<?php

namespace pos\services\admin;

use pos\extensions\ApiBaseService;
use common\models\config\AdminUserModel;
/**
 * Desc 首页服务类
 * @author WMX
 */
class AdminService extends ApiBaseService
{
   
    /**
     * 登录
     * @param array $params 参数 
     * * */
    public static function login($params)
    {
       
        $admin = AdminUserModel::getDataOne(['account' => $params['phone']]);
        if (empty($admin)) {
            return self::jsonError('账号异常，请检查');
        }
        if ($admin['pass'] != md5(trim($params['pass']))) {
            return self::jsonError('密码异常，请检查');
        }
        $token = md5($params['account'] . $admin['pass']);
        unset($admin['pass']);
        $data = [
            'token' => $token,
            'name' => $admin['name']
        ];
        // 获取权限
        \common\services\cache\CommonCache::setCache($token, $admin);
        return self::jsonSuccess($data);
    }


    // 用户信息
    public static function info()
    {
        // 读取用户信息
        $headers = \Yii::$app->getRequest()->getHeaders();
        if (empty($headers->get('token'))) {
            return [];
        }
        $admin = \common\services\cache\CommonCache::getCache($headers->get('token'));
        if(!empty($admin)) {
            return self::jsonSuccess($admin);
        } else {
            return self::jsonError('用户异常，请重新登陆', [], 202);
        }

    }

}
